package com.kpiJavaTraining.bogdan.module2;

import java.io.File;
import java.io.IOException;
import java.util.Date;

public class Statistic {

	private Printer printer = new Printer();
	private final String PATH = "Statistic/";
	private final String FORMAT = ".stat";
	
	public void writeStatistic(User user , int score) throws IOException{
		FileWriter writer = new FileWriter(PATH+user+FORMAT);
		writer.write("Date: " + new Date(System.currentTimeMillis()) +"  Score: "+ score);
		writer.close();
	}
	
	public void printStatistic(User user) throws IOException{
		
		File file = new File(PATH+user+FORMAT);
		if(file.exists()){
			FileReader reader = new FileReader(file.toString());
			while(reader.hasNext()){
				printer.println(reader.read());
			}
			reader.close();
		}
	}
	
	public void printAllUsersStatistic() throws IOException{
		
		File file = new File(PATH);	
		String[] files = file.list();
		for(String name : files){
			System.out.println("--------------------------------------------");
			FileReader fReader = new FileReader(file+"/"+ name);
			System.out.println(name.substring(0,name.length() - 5));
			System.out.println();
			while(fReader.hasNext()){
				System.out.println(fReader.read());
			}
			System.out.println("--------------------------------------------");
		}
	}
}
